import { getCurrentInstance, ref } from 'vue'
import { onBeforeRouteLeave } from 'vue-router'

export const KeepAliveInclude = ref([
  'qyproject-own',
  'qyproject-report',
  'qydocking-resources',
  'qyknowledge-policy',
  'qyknowledge-handle',
  'qypolicy-list',
  'zfcultivate-report',
  'zfcultivate-financial',
  'zfcultivate-bank',
  'zfdocking-list',
  'zfparty-list',
  'zfpolicy-list',
  'zftalent-list',
  'zfneed-demand',
  'zfknowledge-list',
  'zfsafety-list',
  'zflaw-list',
  'zftop-list',
  'zfenterprise-list',
  'zfnotice-list',
  'zfcultivate-list',
  'zfeconomicenterprise-list',
  'zfnotice-submit',
  'zfgovernment-list',
  'zfcockpit-map',
  'zfeconomic-industrial',
  'zfeconomic/economic-details-list',
  'qytalent-demand',
])
export const KeepAliveExcludes = ref([])
export function removeKeepAliveCache(instance) {
  KeepAliveExcludes.value.push(instance.type.name)
  // console.log(excludes.value, 'remove')
}
export function resetKeepAliveCache(instance) {
  KeepAliveExcludes.value = KeepAliveExcludes.value.filter(
    (item) => item !== instance.type.name
  )
  // console.log(excludes.value, 'reset')
}

export function useKeepAliveCache() {
  const instance = getCurrentInstance()
  onBeforeRouteLeave((to, from) => {
    if (
      (from.meta.module &&
        to.meta.module &&
        from.meta.module === to.meta.module) ||
      to.name == 'zfenterprise-enterprise'
    ) {
      if (from.name) {
        // console.log('缓存' + from.name)
        resetKeepAliveCache(instance)
      }
    } else {
      if (from.name) {
        // console.log('不缓存' + from.name)
        removeKeepAliveCache(instance)
      }
    }
  })
  return {
    instance,
  }
}
